/* business.java
 *
 * Project/Product: GW
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   Administrator	2008-7-8	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2008
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package system.download;

import java.io.File;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import oracle.sql.CHAR;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import system.base.ServerDownupfileInfo;
import system.base.ServerDownupfileInfoSrv;
import system.data.ServerDownupfileInfoBase;
import system.data.ServerDownupfileInfoPageData;
import framework.core.*;

// T_SERVER_DOWNUPFILE_INFO ServerDownupfileInfo
/** 机务系统业务类 */
public class Business {
    private static final Business business = new Business();
    private DBConnection con = null;
    String start = " 00:00:00";
    String end = " 23:59:59";

    private Business() {

    }

    /** 获得前一天 ****-**-** */
    String getPreDay() {
        DateUtil du = new DateUtil();
        DateUtil du1 = null;
        try {
            du1 = du.getPreDay();
        } catch (Exception e) {
            
            e.printStackTrace();
        }
        return du1.getDateString();
    }

    /** 获得前一天 YYYYMMDD */
    String getPreDayNum() {
        String preDay = getPreDay();
        String newString = preDay.substring(0, 4);
        newString += preDay.substring(5, 7);
        newString += preDay.substring(8, 10);
        return newString;
    }

    /** 获得格式为YYYYMMDDHHMMSS的时间*/
    String get14Time(String time) {
        String need = "";
        time = time.replace('-', ' ');
        time = time.replace(':', ' ');
        char[] needs = time.toCharArray();
        for (int i = 0; i < needs.length; i++) {
            if (needs[i] != ' ') {
                need += String.valueOf(needs[i]);
            }
        }
        return need;
    }

    /** 处理总方法 (最终系统自动运行)*/
    void mainCreateMethod() {
        String path = FileBusiness.getRealPath();
        boolean document = true;
        if (!FileBusiness.isFileExists(path)) {
            document = FileBusiness.createDocument(path);
        }
        if(!document)
            return;
        try {
            con = DBConnection.open(false);
            con.setAutoCommit(false);
        } catch (Exception e) {
            
            e.printStackTrace();
        }
        //创建对象
        ProductBusiness pb = new ProductBusiness();
        ProductCategoryBusiness pcb = new ProductCategoryBusiness();
        OutListBusiness olb = new OutListBusiness();
        //获得时间
        String preDay = this.getPreDay();
        //查询   处理  写文件(问题:获得目录;检索目录;写文件;)
        pb.searchProduct(preDay, con);
        pcb.searchCategory(preDay, con);
        olb.searchOuthead(preDay, con);
        try {
            con.commit();
            System.out.println("system.download.Business.mainCreateMethod()-----------成功运行");
        } catch (Exception e) {
            try {
                con.rollback();
                System.out.println("system.download.Business.mainCreateMethod()-----------运行失败------成功回滚");
            } catch (Exception e1) {
                
                e1.printStackTrace();
                System.out.println("system.download.Business.mainCreateMethod()-----------运行失败------回滚失败");
            }
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Business b = null;
        b = Business.business;
        b.mainCreateMethod();
        DateUtil du = new DateUtil();
        DateUtil du1 = null;
        try {
            du1 = du.getPreDay();
        } catch (Exception e) {
            
            e.printStackTrace();
        }
        try {
            System.out.println(du1.getDateEndStr());
            System.out.println(du1.getMonthStart());
        } catch (Exception e) {
            
            e.printStackTrace();
        }
        System.out.println();// ////////////
        System.out.println(b.getPreDay());
        System.out.println(du.getFullString());
        File f = new File(".");
        f.getPath();
        URL url = ClassLoader.getSystemClassLoader().getSystemResource("//");
        String path = url.getPath(); //获得当前类所在的路
        System.out.println(path);
        ;
        System.out.println(b.get14Time("2008-07-10 15:55:01"));
    }

    /** 其他方法 **************************************************************** */
    /** 创建用于写报告的数据 */
    Map createMap(String warehouseCode, String dataTableName, String path, String filename, String state) {
        Map map = new HashMap();
        map.put("DOWNLOADTYP", "下载");
        map.put("WAREHOUSE_UINCODE", "" + warehouseCode);
        map.put("DATATABLE_NAME", dataTableName);
        map.put("PARMSTYPE", "TEXT");
        map.put("PARMSSTR", path + filename);
        map.put("UPDOWN_STATE", state);
        map.put("STATUS", "1");
        map.put("CREATE_USER_ID", "JWXT");
        map.put("CREATE_USER", "JWXT");
        DateUtil date = new DateUtil();
        String time = date.getFullString();
        map.put("CREATE_TIME", time);
        map.put("LAST_UPDATE_USER_ID", "JWXT");
        map.put("LAST_UPDATE_USER", "JWXT");
        map.put("LAST_UPDATE_TIME", time);
        return map;
    }

    /** 访问数据库表 T_SERVER_DOWNUPFILE_INFO 填写报告 */
    boolean writeReport(DBConnection con, Map map) {
        ServerDownupfileInfo info = ServerDownupfileInfo.fetch();
        ServerDownupfileInfoPageData data = new ServerDownupfileInfoPageData();
        data.setDownloadtyp(String.valueOf(map.get("DOWNLOADTYP")));
        data.setWarehouseUincode(String.valueOf(map.get("WAREHOUSE_UINCODE")));
        data.setDatatableName(String.valueOf(map.get("DATATABLE_NAME")));
        data.setParmstype(String.valueOf(map.get("PARMSTYPE")));
        data.setParmsstr(String.valueOf(map.get("PARMSSTR")));
        data.setUpdownState(String.valueOf(map.get("UPDOWN_STATE")));
        data.setStatus(String.valueOf(map.get("STATUS")));
        data.setCreateUserId(String.valueOf(map.get("CREATE_USER_ID")));
        data.setCreateUser(String.valueOf(map.get("CREATE_USER")));
        data.setCreateTime(String.valueOf(map.get("CREATE_TIME")));
        data.setLastUpdateUserId(String.valueOf(map.get("LAST_UPDATE_USER_ID")));
        data.setLastUpdateUser(String.valueOf(map.get("LAST_UPDATE_USER")));
        data.setLastUpdateTime(String.valueOf(map.get("LAST_UPDATE_TIME")));
        try {
            info.fill(data);
            info.save(con);
        } catch (Exception e) {
            
            e.printStackTrace();
        }
        return true;
    }

    // add by Wing start
    HSSFCell createCell(HSSFWorkbook aWb, HSSFRow aRow, short column, String aValue) {
        HSSFCell tCell = aRow.createCell(column);
        tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        tCell.setCellValue(aValue);
        return tCell;
    }

    /** 查询并返回以map为元素的vector集合 */
    Vector setMap(String[] colNames, String sql, DBConnection con) {
        int size = colNames.length;
        DBResult dbrs = null;
        ResultSet rs = null;
        Vector vec = null;
        try {
            dbrs = DBEngine.doSelect(con, sql);
            rs = dbrs.getRs();
            vec = new Vector();
            while (rs.next()) {
                Map map = new HashMap();
                for (int i = 0; i < size; i++) {
                    map.put(colNames[i], rs.getString(colNames[i]));
                }
                vec.add(map);
            }
        } catch (SQLException e) {
            
            e.printStackTrace();
        } catch (Exception e) {
            
            e.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (SQLException e) {
                
                e.printStackTrace();
            }
            try {
                if (dbrs != null)
                    dbrs.clear();
            } catch (Exception e) {
                
                e.printStackTrace();
            }
        }
        return vec;
    }

    /**
     * @return the business
     */
    public static Business getBusiness() {
        return business;
    }
    
}
